package com.edu.d9_map_impl;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

public class TreeMapDemo {
    public static void main(String[] args) {
        //TreeSet和TreeMap一样，会按照大小进行一个自动排序，TreeSet是直接排序，TreeMap是按照1键进行升序排列
        Map<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(22,"胡立博");
        treeMap.put(21,"欧阳怡婷");
        treeMap.put(33,"陈鑫");
        treeMap.put(11,"胡启航");
        System.out.println(treeMap);

        //TreeMap带自动排序，可排序，不重复，无索引，同时还可以使用两种自定义排序规则方法（跟TreeSet和Collections两种自定义规则一样）
        /**
         * 方法二：已经实现了comparable<>接口，并且已经重写了compareTo方法，自定义了比较规则
         * 也可以使用方法一：TreeMap自带的比较器，定义比较规则
         * 当两种规则同时定义了之后，优先使用TreeMap比较器自带的方法一，就近原则
         */
/*        Map<Apple, String> treeMap1 = new TreeMap<>(new Comparator<Apple>() {
            @Override
            public int compare(Apple apple, Apple t1) {
                return Double.compare(apple.getPrice(),t1.getPrice());
            }
        });*/
//Lanbda表达式简化后的代码
        Map<Apple, String> treeMap1 = new TreeMap<>((apple, t1)->Double.compare(apple.getPrice(),t1.getPrice()));

        treeMap1.put(new Apple("富士山","红苹果",99.8,100),"云南");
        treeMap1.put(new Apple("山","青苹果",66.3,600),"北京");
        treeMap1.put(new Apple("士山","黄苹果",22.5,300),"上海");
        treeMap1.put(new Apple("富","黑苹果",77.4,300),"湖北");
        System.out.println(treeMap1);
    }
}
